﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Threading;

namespace MDX_Studio_v2
{
	public partial class QueryEditor : DockContent
	{
		public string Query
		{
			get
			{
				if (codeEditorControl.Selection.Text.Length > 0) return codeEditorControl.Selection.Text;
				return codeEditorControl.Document.Text;
			}

			set
			{
				codeEditorControl.Document.Text = value;
			}
		}

		private ParametersWindow currWin;
		public void SetParameterWindow(ParametersWindow p)
		{
			currWin = p;
		}

		private QueryEditorInfo _info;
		public QueryEditorInfo Info { get { return _info; } set { _info = value; } }
		private BackgroundWorker worker;

		public void InvokeExecution()
		{
			worker = new BackgroundWorker();
			worker.DoWork += new DoWorkEventHandler(BackgroundDoWork);
			worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkDone);
			worker.WorkerSupportsCancellation = true;
			worker.RunWorkerAsync();
		}
		public void CancelExecution()
		{
			if (worker != null && worker.IsBusy) worker.CancelAsync();
		}

		private void BackgroundDoWork(object sender, DoWorkEventArgs e)
		{
			/*try
			{
				_Context.Connection.StartQueryConnection();

				AdomdConnection queryConn = _Context.Connection.QueryConnection;

				AdomdCommand command = queryConn.CreateCommand();
				command.CommandText = new ParseQuery().Parse(queryEditors[activeQueryEditor].Query);

				// Perf
				_Context.PerfStats.Start(_Context.Connection.ServerName, "2008");
				CellSet set = command.ExecuteCellSet();
				_Context.PerfStats.Finish();

				// Update
				_resultWindow.Results = set;
				PropagateContext();
			}
			catch (Exception ex)
			{
				Console.WriteLine("MainForm.cs\\toolstrip_Execute_Click: {0}", ex.Message);
				_resultWindow.AddMessage(ex.Message);
			}*/
		}

		private void BackgroundWorkDone(object sender, RunWorkerCompletedEventArgs e)
		{

		}

		/// <summary>
		/// Constructor
		/// </summary>
		public QueryEditor()
		{
			InitializeComponent();
			codeEditorControl.Document.SyntaxFile = "MDX";
		}

		public QueryEditor(QueryEditorInfo info)
		{
			InitializeComponent();
			codeEditorControl.Document.SyntaxFile = "MDX";

			// TODO: Check if filename provided
			//			Load if required
			//			Update Query, and Title
		}

		/// <summary>
		/// Event that fires when this "query" receives focus
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void QueryEditor_Activated(object sender, EventArgs e)
		{
			// TODO: switch settings in-and-out when a separate window has focus
		}

		private void QueryEditor_Load(object sender, EventArgs e)
		{
			this.Text = this.Info.Title;
		}

		private void codeEditorControl_TextChanged(object sender, EventArgs e)
		{
			/*this.Info.UnsavedChanges = true;
			// TODO propogate to title
			this.Text = this.Info.Title;*/
			if(currWin != null) currWin.QueryText = Query ;
		}
	}
}
